package com.hula.onion.book.server.spider.platforms.shengcaijinrong;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/17 19:49
 *
 * @author shan
 * @version 1.0
 */
@Component
public class ShengCaiJinRongSpiderHelper {

    /**
     * 登录组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form, String sessionId, String token) {

        Map<String, Object> dataMap = new HashMap<>(4);
        dataMap.put("mobile", form.getUserName());
        dataMap.put("password", form.getPassword());
        dataMap.put("session_id", sessionId);
        dataMap.put("token", token);

        Map<String, Object> temMap = new HashMap<>(4);
        temMap.put("method", "login");
        temMap.put("source", "pc");
        temMap.put("version", "2.0.0.0");
        temMap.put("data", dataMap);

        Map<String, Object> map = new HashMap<>(1);
        map.put("argv", new Gson().toJson(temMap));
        return map;

    }

    /**
     * 查询账户组装请求参数
     *
     * @param
     * @return
     */
    public Map<String, Object> installRequestParams(String sessionId, String token) {

        Map<String, Object> dataMap = new HashMap<>(3);
        dataMap.put("type", "3");
        dataMap.put("session_id", sessionId);
        dataMap.put("token", token);

        Map<String, Object> temMap = new HashMap<>(5);
        temMap.put("url", "roadoor-user-biz/acc/queryBal");
        temMap.put("method", "queryBal");
        temMap.put("source", "pc");
        temMap.put("version", "2.0.0.0");
        temMap.put("data", dataMap);

        Map<String, Object> map = new HashMap<>(1);
        map.put("argv", new Gson().toJson(temMap));
        return map;

    }

    /**
     * 解析账户余额
     *
     * @param json
     * @return
     */
    public CommonResponse parseAccountBalance(String json) {

        try {
            AccountInfo accountInfo = new AccountInfo();

            JSONObject funInfo = JSON.parseObject(json)
                    .getJSONObject("data").getJSONObject("result");
            accountInfo.setAmount(funInfo.getBigDecimal("totalBalance"));
            accountInfo.setWaitIntrest(funInfo.getBigDecimal("unReturnedTotalInterest"));
            accountInfo.setTotalIntrest(funInfo.getBigDecimal("returnedTotalInterest"));
            return new CommonResponse(accountInfo);
        } catch (Exception e) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }

    }


}
